package com.fmsh.nfcsensor.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.fmsh.base.adapter.BaseRecyclerAdapter;
import com.fmsh.base.utils.UIUtils;
import com.fmsh.nfcsensor.R;
import com.fmsh.nfcsensor.bean.SensorBean;
import com.fmsh.nfcsensor.utils.Constants;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author wuyajiang
 * @date 2022/11/7
 */
public class SensorAdapter extends BaseRecyclerAdapter {
    public SensorAdapter(Context context) {
        super(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public int getLayoutId() {
        return R.layout.iterm_sensor;
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View view) {
        View loadView = UIUtils.loadView(mContext, R.layout.iterm_sensor, null);
        return new ViewHolder(loadView);
    }

    @Override
    public void setBindHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        SensorBean sensorBean = (SensorBean) mList.get(position);
        viewHolder.iv.setImageResource(sensorBean.getImageId());
        viewHolder.type.setText(sensorBean.getTypeId());
        if(sensorBean.getType() == Constants.P_VALUE || sensorBean.getType() == Constants.VOC_VALUE ||
                sensorBean.getType() == Constants.NOX_VALUE|| sensorBean.getType() == Constants.CO2_VALUE){

            viewHolder.value.setText(String.format("%.0f  %s",sensorBean.getValue(),sensorBean.getUnity()));
        }else {
            viewHolder.value.setText(String.format("%.2f  %s",sensorBean.getValue(),sensorBean.getUnity()));
        }

        viewHolder.hint.setText(sensorBean.getDetail());

    }

    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv)
        ImageView iv;
        @BindView(R.id.value)
        TextView value;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.hint)
        TextView hint;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
